package com.hc.likelistenmusic.adapter

import android.content.Context
import android.graphics.Color
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.hc.likelistenmusic.R
import com.hc.likelistenmusic.bean.LocalMusic
import java.io.Serializable

class LocalMusicFilterAdapter(
    var context: Context?,
    var layoutId: Int,
    var list: List<LocalMusic>
) :
    BaseAdapter<LocalMusic>(context, layoutId, list), Serializable {
    override fun bindViews(holder: BaseHolder, t: LocalMusic, position: Int) {
        var cl_ilm_root = holder.getView<ConstraintLayout>(R.id.cl_fmrfi_root)
        var tv_ilm_position = holder.getView<TextView>(R.id.tv_fmrfi_position)
        var tv_ilm_name = holder.getView<TextView>(R.id.tv_fmrfi_name)
        var tv_ilm_singer = holder.getView<TextView>(R.id.tv_fmrfi_singer)
        var tv_ilm_duration = holder.getView<TextView>(R.id.tv_fmrfi_duration)
        (position + 1).toString().let {
            tv_ilm_position?.text = it
            tv_ilm_position?.tag = position
            cl_ilm_root?.tag = list[position]
        }
        list[position].let {
            tv_ilm_name?.text = it.name
            tv_ilm_singer?.text = "${it.singer} - ${it.album} - ${it.year}"
            tv_ilm_duration?.text = it.durationString
            when (it.selectedType) {
                1 -> {
                    tv_ilm_name.isSelected = false
                    tv_ilm_singer.isSelected = false
                    tv_ilm_position?.setTextColor(Color.WHITE)
                    tv_ilm_name?.setTextColor(Color.WHITE)
                    tv_ilm_singer?.setTextColor(Color.WHITE)
                    tv_ilm_duration?.setTextColor(Color.WHITE)
                }

                2 -> {
                    tv_ilm_name.isSelected = true
                    tv_ilm_singer.isSelected = true
                    tv_ilm_position?.setTextColor(Color.GREEN)
                    tv_ilm_name?.setTextColor(Color.GREEN)
                    tv_ilm_singer?.setTextColor(Color.GREEN)
                    tv_ilm_duration?.setTextColor(Color.GREEN)
                }

                else -> {
                    tv_ilm_name.isSelected = false
                    tv_ilm_singer.isSelected = false
                    tv_ilm_position?.setTextColor(Color.RED)
                    tv_ilm_name?.setTextColor(Color.RED)
                    tv_ilm_singer?.setTextColor(Color.RED)
                    tv_ilm_duration?.setTextColor(Color.RED)
                }
            }
        }
    }

    override fun bindListener(holder: BaseHolder) {
        var cl_ilm_root = holder.getView<ConstraintLayout>(R.id.cl_fmrfi_root)
        var tv_ilm_position = holder.getView<TextView>(R.id.tv_fmrfi_position)
        cl_ilm_root?.setOnClickListener {
            onItemClickListen?.onItemClick(
                cl_ilm_root?.tag as LocalMusic,
                tv_ilm_position?.tag as Int
            )
        }
    }
}